<?php
	date_default_timezone_set('Europe/Paris');
	include('includes/connexion.php');
	$elevemanager = new EleveManager($connexion);
	session_start();
	
	$monResultat = new Resultat();
	$resultatmanager = new ResultatManager($connexion);
	$exercicemanager = new ExerciceManager($connexion);
	$questionmanager = new QuestionManager($connexion);
	$reponsemanager = new ReponseManager($connexion);
	
	$type = $exercicemanager->get($_POST['idExercice'])->type();

	//Si l'exercice est de type DOMINO
	if($type == 'D')
	{
		foreach($questionmanager->getByExercice($_POST['idExercice']) as $quest){
			if($_POST['domino1quest'.$quest->id()] != '' && $_POST['domino2quest'.$quest->id()] != '')
			{
				$reponseDonnee = $_POST['domino1quest'.$quest->id()]."/".$_POST['domino2quest'.$quest->id()];
				$reponseJuste = $reponsemanager->getByQuestion($quest->id());
				if($reponseDonnee == $reponseJuste[0]->contenu())
				{
					$monResultat->setIdRefReponse($reponseJuste[0]->id());
				}
				else
				{
					foreach($reponsemanager->getByQuestion($quest->id()) as $rep)
					{
						if($rep->correcte() == "0")
						{
							$monResultat->setIdRefReponse($rep->id());
							break;
						}
					}
				}
				$monResultat->setIdRefEleve($_SESSION['Eleve']->id());
				$monResultat->setIdRefTest($_POST['idRefTest']);
				$secondes = $_POST['minutes']*60 + $_POST['secondes'];
				$monResultat->setTemps($secondes);
				$resultatmanager->add($monResultat);
					
			}
		}
	}	

	
//Si l'exercice est de type relation, analogie, series numeriques, briques et blocs ou générique
if($type == "R" || $type == "A" || $type == "SN" || $type == "BB" || $type == "G")
{
	foreach($_POST as $key => $value){
		if($key != 'idRefTest' && $key != 'minutes' && $key != 'secondes'){
			$monResultat->setIdRefEleve($_SESSION['Eleve']->id());
			$monResultat->setIdRefReponse($value);
			$monResultat->setIdRefTest($_POST['idRefTest']);
			$secondes = $_POST['minutes']*60 + $_POST['secondes'];
			$monResultat->setTemps($secondes);
			$resultatmanager->add($monResultat);
		}
	}
}

//Si l'exercice est de type texte a trou
if($type == "TT"){
	$reponseTT = new ReponseTxtTrousPositionManager($connexion);
	foreach($_POST as $key => $value)
	{
		if($key != 'idRefTest' && $key != 'minutes' && $key != 'secondes' && $key != 'idExercice')
		{
			foreach($value as $key2 => $value2)
			{
				$idQuestion = explode('mot', $key);
				$idQuestion = $idQuestion[1];
				
				$idReponse = $value2;
				
				$position = $key2;
				
				//Si la réponse est correctement placée
				if($reponseTT->check($idQuestion, $idReponse, $position)){
					$monResultat->setIdRefEleve($_SESSION['Eleve']->id());
					$monResultat->setIdRefReponse($idReponse);
					$monResultat->setIdRefTest($_POST['idRefTest']);
					$secondes = $_POST['minutes']*60 + $_POST['secondes'];
					$monResultat->setTemps($secondes);
					$resultatmanager->add($monResultat);
				} else { //Sinon
					if($reponseTT->getReponseFausse($idQuestion, $idReponse) != false)
					{
						$maReponse = $reponseTT->getReponseFausse($idQuestion, $idReponse);
						if($maReponse->correcte() != 0)
						{
							$maReponse->setCorrecte(0);
							$reponsemanager->update($maReponse);
						}
							$monResultat->setIdRefEleve($_SESSION['Eleve']->id());
							$monResultat->setIdRefReponse($maReponse->id());
							$monResultat->setIdRefTest($_POST['idRefTest']);
							$secondes = $_POST['minutes']*60 + $_POST['secondes'];
							$monResultat->setTemps($secondes);
							$resultatmanager->add($monResultat);
					}
				}
			}
		}
	}
}
	header('Location:go.php');
?>